package cn.iocoder.yudao.framework.jackson.config;

import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.jackson.core.EnhancedLocalDateDeserializer;
import cn.iocoder.yudao.framework.jackson.core.EnhancedLocalDateTimeDeserializer;
import cn.iocoder.yudao.framework.jackson.core.EnhancedLocalTimeDeserializer;
import cn.iocoder.yudao.framework.jackson.core.BigDecimalSerializer;
import cn.iocoder.yudao.framework.jackson.core.EnhancedLocalDateSerializer;
import cn.iocoder.yudao.framework.jackson.core.EnhancedLocalDateTimeSerializer;
import cn.iocoder.yudao.framework.jackson.core.EnhancedLocalTimeSerializer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@AutoConfiguration
@Slf4j
public class YudaoJacksonAutoConfiguration {

    /**
     *  TODO 重构
     * @see org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration
     */
    @Bean
    @Lazy(false)
    @SuppressWarnings({"InstantiationOfUtilityClass"})
    public JsonUtils jsonUtils(ObjectMapper objectMapper) {
        // 不忽略 null 值，不需要的字段可用 @JsonIgnore
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
        // 序列化时当找不到类型的访问器是否引发异常
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // 遇到未知属性是否应导致反序列化失败
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // 支持 java.time.* 类序列化及反序列化
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.registerModule(new ParameterNamesModule());
        objectMapper.registerModule(new SimpleModule()
                // Long 转换成 String，避免前端 JS 溢出问题
                .addSerializer(Long.class, ToStringSerializer.instance)
                .addSerializer(Long.TYPE, ToStringSerializer.instance)
                // BigInteger 转换成 String，避免前端 JS 溢出问题
                .addSerializer(BigInteger.class, ToStringSerializer.instance)
                // BigDecimal 转换成 String 并格式化数值，避免前端 JS 溢出问题
                .addSerializer(BigDecimal.class, BigDecimalSerializer.INSTANCE)
                // 新增 LocalDate 序列化、反序列化规则
                .addSerializer(LocalDate.class, EnhancedLocalDateSerializer.INSTANCE)
                .addDeserializer(LocalDate.class, EnhancedLocalDateDeserializer.INSTANCE)
                // 新增 LocalTime 序列化、反序列化规则
                .addSerializer(LocalTime.class, EnhancedLocalTimeSerializer.INSTANCE)
                .addDeserializer(LocalTime.class, EnhancedLocalTimeDeserializer.INSTANCE)
                // 新增 LocalDateTime 序列化、反序列化规则
                .addSerializer(LocalDateTime.class, EnhancedLocalDateTimeSerializer.INSTANCE)
                .addDeserializer(LocalDateTime.class, EnhancedLocalDateTimeDeserializer.INSTANCE)
        );
        JsonUtils.init(objectMapper);
        log.info("[JsonUtils] Jackson#ObjectMapper {} init success", objectMapper.version());
        return new JsonUtils();
    }

}
